//
//  GetAddressTableViewController.m
//  1243
//
//  Created by 袖手旁觀 on 16/4/14.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "GetAddressTableViewController.h"
#import "AddressTableViewCell.h"
#import "NetWorkTools.h"

@interface GetAddressTableViewController ()

@property (nonatomic,strong) NSMutableArray *addressArray;

@end

@implementation GetAddressTableViewController

- (NSMutableArray *)addressArray {
    if (!_addressArray) {
        _addressArray = [NSMutableArray array];
    }
    return _addressArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"收货地址";
    
    self.tableView.rowHeight = 100;
    
    [self loadUserAddressData];
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.addressArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"OrderDetailTableViewCellget";
    AddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"AddressTableViewCell" owner:nil options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
    NSString *NameString = self.addressArray[indexPath.row][@"name"];
    cell.NameLabel.text = [NSString stringWithFormat:@"姓名: %@",NameString];
    
    NSString *PhoneNum = self.addressArray[indexPath.row][@"phone"];
    cell.PhoneNameLabel.text = [NSString stringWithFormat:@"电话: %@",PhoneNum];
    
    NSString *AddressString = [NSString stringWithFormat:@"%@ %@ %@ %@ ",  self.addressArray[indexPath.row][@"province"], self.addressArray[indexPath.row][@"city"],self.addressArray[indexPath.row][@"county"],self.addressArray[indexPath.row][@"address"]];
    cell.AddressLabel.text = [NSString stringWithFormat:@"详细地址: %@",AddressString];
    
    
    return cell;
}


#pragma mark - 获取数据

- (void)loadUserAddressData {
        AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
        mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
        NSDictionary *param = @{@"username":@"18620614920"};
    
    [mgr GET:@"http://www.ylgw8.com/AppUser/Getdz" parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.addressArray  = [self toArrayOrNSDictionary:responseObject][@"data"];
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}



@end
